import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Gao zhao xu
 * Date: 2023-10-12
 * Time: 22:05
 */
//用队列实现栈
public class MyStack {
    public Queue<Integer> qu1;
    public Queue<Integer> qu2;

    public MyStack(){
        this.qu1 = new LinkedList<>();
        this.qu2 = new LinkedList<>();
    }
    public void push(int value) {
        if (!qu1.isEmpty()) {
            qu1.offer(value);
        } else if (!qu2.isEmpty()) {
            qu2.offer(value);
        } else {
            qu1.offer(value);
        }
    }


    public int pop() {
        if (empty()){
            return -1;
        }
        if (!qu1.isEmpty()) {
            int count = qu1.size();
            for (int i = 0; i < count - 1; i++) {
                int x = qu1.poll();
                qu2.offer(x);
            }
            return qu1.poll();
        } else if (!qu2.isEmpty()) {
            int count = qu2.size();
            for (int i = 0; i < count - 1; i++) {
                int x = qu2.poll();
                qu1.offer(x);
            }
            return qu2.poll();
        }
        return -1;
    }

    public int peek() {
        if (empty()){
            return -1;
        }
        if (!qu1.isEmpty()) {
            int x = -1;
            int count = qu1.size();
            for (int i = 0; i < count; i++) {
                x = qu1.poll();
                qu2.offer(x);
            }
            return x;
        } else if (!qu2.isEmpty()) {
            int x = -1;
            int count = qu2.size();
            for (int i = 0; i < count; i++) {
                x = qu2.poll();
                qu1.offer(x);
            }
            return x;
        }
        return -1;
    }

    public boolean empty() {
        return qu1.isEmpty() && qu2.isEmpty();
    }
}
